Lab5


4531203821_4531204421  นาย จิรเดช วทัญญุตานนท์ และ นาย จิรวุฒิ จึงศิรกุลวิทย์ (8/13/2002 (11:38:10 AM))
(SM=3, CM=11, ST=15, KY=0, TR=02:49)

TestScript
Mini-Quiz :  (0.0 คะแนน)

JLab>java Selftest
>>JLabIO->Testing (2017, 7) : 2.00
>>JLabIO->Testing (2025, 1) : 2.00
>>JLabIO->Testing (2017, 4) : 2.00
>>JLabIO->Testing (2027, 5) : 2.00
>>JLabIO->Testing (2019, 4) : 2.00

>>JLab:<POINT>10.00</POINT>
JLab>

ได้ 10 คะแนน
Source Code
/**
 * 2110101 Computer Programming
 * Lab #5 : Loops : while, do-while, for
 */
import java.util.*;
import jlab.JLabIO;

public class Lab5 {
  public static void calendar(int year, int month) {
    // Example
    //-----------------------------
    //        August(2002)
    //-----------------------------
    // SUN MON TUE WED THU FRI SAT
    //                   1   2   3     \
    //   4   5   6   7   8   9  10      |
    //  11  12  13  14  15  16  17       >  your code
    //  18  19  20  21  22  23  24      |
    //  25  26  27  28  29  30  31     /
    //=============================
    
    // print header
    System.out.println(LONGLINE);
    System.out.println("        " + monthName[month - 1] + "(" + year + ")");
    System.out.println(LONGLINE);
    System.out.println(" SUN MON TUE WED THU FRI SAT");
    
    Calendar calendar = new GregorianCalendar(new Locale("th", "TH"));
    calendar.set((year > 2500 ? year - 543 : year), month - 1, 1);
    int firstDay = calendar.get(Calendar.DAY_OF_WEEK);
    int numDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    
    // add your code here to format and print the month
    // Use the following two variables :
    //   - numDays is the number of days in this month
    //   - firstDay is the day-of-week of the first of this month
    
    int d = 1;
    for (int i = 1; i <= firstDay-1; i++) {
      System.out.print("    ");}
    for (int i = 1; i <= 8 - firstDay; i++) {
      System.out.print(JLabIO.format(d, 4));
      d++;}
      System.out.println("");
      while (d <= numDays) {

        for (int i = 1; i <= 7; i++) {
        if (d <= numDays) {
        System.out.print(JLabIO.format(d, 4));
        d++; }
      }
      System.out.println("");
    
     } 
    
    // add a line to notify the end of printing
    System.out.println(LASTLINE);
  }
  
  //----------------------------------------------------------------
  public static void main(String[] args) {
    int y = JLabIO.readInt("Year = ");
    int m = JLabIO.readInt("Month = ");

    if (y > 0 && (1 <= m && m <= 12)) {
      calendar(y, m);
    } else {
      System.out.println("error : invalid year or month");
    }
  }
  //----------------------------------------------------------------
  
  final static String[] monthName =
     {"January", "February", "March", "April",
      "May", "June", "July", "August",
      "September", "October", "November", "December"};
  final static String LONGLINE = "-----------------------------";
  final static String LASTLINE = "=============================";
  
}